package com.woniuxy.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.Club;
import com.woniuxy.yoga.pojo.Course;
import com.woniuxy.yoga.service.ClubService;
import com.woniuxy.yoga.service.CourseService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y_Chi
 * @since 2020-10-23
 */
@RestController
@RequestMapping("/yoga/club")
public class ClubController {


    @Autowired
    private ClubService clubService;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "展示俱乐部")
    @GetMapping(value = "/getAllClub")
    @CrossOrigin
    public R getAllClub() {
        List<Club> list = clubService.list();
        if (list != null) {
            return R.ok().data("clubs", list);
        } else {
            return R.error().message("无法查询俱乐部");
        }
    }

    @ApiOperation(value = "俱乐部分页")
    @GetMapping(value = "/clubByPage")
    @CrossOrigin
    public R clubByPage(@ApiParam(value = "当前页") @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
                        @ApiParam(value = "每页的记录数") @RequestParam(value = "limit", required = true, defaultValue = "6") Integer limit) {
        //创建Page对象
        Page pageBean = new Page<>(page,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        Page resultPage = clubService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if(records!=null){
            return R.ok().data("clubs",records).data("total",total);
        }else{
            return R.error().message("没有查到集合数据");
        }
    }
    @ApiOperation(value = "得到单个俱乐部")
    @GetMapping("/getClubById/{id}")
    @CrossOrigin
    public R getClubById(@PathVariable("id")Integer id){
        System.out.println(id);
        Club club = clubService.getById(id);
        Course c=new Course();
        c.setClubid(id);
        QueryWrapper<Course> q=new QueryWrapper<Course>(c);
        List<Map<String, Object>> course = courseService.listMaps(q);
        if (club != null) {
            return R.ok().data("club", club).data("course",course);
        } else {
            return R.error().message("无法查询俱乐部");
        }
    }
}

